package com.crcode.crm.controller.Industry;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crcode.crm.bean.Industry.IndustryBean;
import com.crcode.crm.service.Industry.IndustryService;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultPageUtil;
import com.crcode.crm.utils.ResultUtil;

/**
 * @author lmz
 * @company Peter
 * @create 2019/3/25  14:03
 */
@Controller
@RequestMapping("/industry")
public class IndustryController {
    @Autowired
    private IndustryService industryService;

    /**
     * 获取所有信息
     *
     * @param request
     * @param response
     * @param param
     * @param vo
     * @return
     */
    @PostMapping(value = "/allinfos")
    @ResponseBody
    public ResultPageUtil findAllIndustry(HttpServletRequest request, HttpResponse response, @RequestParam Map<String, Object> param, PageVo vo) {
        //System.out.println(param.size()+"==="+JSON.toJSONString(param));
        //System.out.println(JSON.toJSONString(vo));
        return industryService.findAllIndustry(param, vo);

    }


    /**
     * 获取树形的接口
     *
     * @param uuid
     * @return
     */
    @RequestMapping("/ztree")
    @ResponseBody
    public List<IndustryBean> loadZtree(@RequestParam(required = false) String uuid) {

        return industryService.loadztree(uuid);
    }

    /**
     * 添加行业
     *
     * @param industryBean
     * @return
     */
    @PostMapping("/addIndustry")
    @ResponseBody
    public ResultUtil addIndustry(IndustryBean industryBean,HttpSession session) {
        //System.out.println(industryBean.getUuid());
        ResultUtil ru = new ResultUtil();
        Object obj = session.getAttribute("user");
        if(obj == null) {
            ru.setStatus(false);
            ru.setInfo("未登录，请先登录");
            return ru;
        }
        ResultUtil resultUtil = industryService.addIndustry(industryBean);
        //System.out.println(JSON.toJSONString(industryBean));
        return resultUtil;
    }

    /**
     * 删除行业
     */
    @PostMapping("/delIndustry")
    @ResponseBody
    public ResultUtil delIndustry(String uuids) {
        //System.out.println(uuid);
        return industryService.deleteIndustry(uuids);
    }


    /**
     * 获取所有行业大类
     * @return List<IndustryBean>
     */
    @RequestMapping(value = "/findAllParent", method = RequestMethod.GET)
    @ResponseBody
    public List<IndustryBean> findAllParent() {
        return industryService.findAllParent();
    }
}
